﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Collections.Specialized;
using WAD.DAL;
using WAD.Model;

namespace WAD.WEB.AppCode
{
    /// <summary>
    /// 后台所有ajax基类
    /// </summary>
    public class AdminAjaxBase : QD.Web.Ajax.AjaxPage
    {
        protected int adminId { set; get; }

        protected MUser userBean;//用户信息

        /// <summary>
        /// 加載時執行
        /// </summary>
        protected override void PageLoad()
        {
            if (!Request.HttpMethod.Equals("POST"))
            {
                //限制请求方式为POST
                //JsonResult(Message.Code.NO_PERMISSION, Message.Msg.NO_PERMISSION);
            }
            adminId = Parse.Int("admin_id", 0);
        }

        /// <summary>
        /// 判断是否登陆
        /// </summary>
        protected void IsLogin()
        {
            
        }

        /// <summary>
        /// 操作成功
        /// </summary>
        protected void Success()
        {
            JsonResult(Message.Code.SUCCESS, Message.Msg.SUCCESS);
        }

        /// <summary>
        /// 操作成功
        /// </summary>
        protected void Success(object item)
        {
            JsonResult(Message.Code.SUCCESS, Message.Msg.SUCCESS, item);
        }

        /// <summary>
        /// 操作失败
        /// </summary>
        protected void Fail()
        {
            JsonResult(Message.Code.FAIL, Message.Msg.FAIL);
        }

        /// <summary>
        /// 沒有数据
        /// </summary>
        protected void DataEmpty()
        {
            JsonResult(Message.Code.DATA_EMPTY, Message.Msg.DATA_EMPTY);
        }

        /// <summary>
        /// 参数错误
        /// </summary>
        protected void ParamError()
        {
            JsonResult(Message.Code.PARAM_ERROR, Message.Msg.PARAM_ERROR);
        }

        /// <summary>
        /// ajax中未捕获的异常处理
        /// </summary>
        /// <param name="e"></param>
        protected override void ErrorCatch(Exception e)
        {
            StringBuilder sb = new StringBuilder();

            sb.Append("Type:").Append(e.GetType().Name).Append("\n");
            sb.Append("Message:").Append(e.Message).Append("\n");
            sb.Append("Class:").Append(this.GetType()).Append("\n");
            sb.Append("URL:").Append(Request.Url).Append("\n");

            //QueryString参数
            NameValueCollection QueryString = Request.QueryString;
            sb.Append("QueryString参数:");
            foreach (string key in QueryString.Keys)
            {
                sb.Append(key).Append(" = ").Append(QueryString[key]).Append("\t"); ;
            }
            sb.Append("\n");

            //Form参数
            NameValueCollection Form = Request.Form;
            sb.Append("Form参数:");
            foreach (string key in Form.Keys)
            {
                sb.Append(key).Append(" = ").Append(Form[key]).Append("\t"); ;
            }
            sb.Append("\n");

            //Cookie参数
            sb.Append("Cookie参数:");
            for (int i = 0; i < HttpContext.Current.Request.Cookies.Count; i++)
            {
                sb.Append(Request.Cookies.Keys[i]).Append(" = ").Append(Request.Cookies[i].Value.ToString()).Append("\t");

            }
            sb.Append("\n");

            //文件列表
            sb.Append("文件列表:");
            HttpFileCollection files = Request.Files;
            for (int i = 0; i < files.Count; i++)
            {
                sb.Append("name = ").Append(files[i].FileName).Append("\t");
            }

            sb.Append("\n");


            SavaLog("Error", sb.ToString());

            JsonResult(Message.Code.SYSTEM_ERROR, Message.Msg.SYSTEM_ERROR);
        }


        /// <summary>
        /// 保存日志
        /// </summary>
        /// <param name="type"></param>
        /// <param name="content"></param>
        protected void SavaLog(String type, String content)
        {
            MLogcat m = new MLogcat() { content = content, type = type };
            DaoLogcat.Add(m);
        }
    }
}